//
//  JNTHomeViewController.m
//  OA
//
//  Created by 王文晓 on 16/8/4.
//  Copyright © 2016年 王文晓. All rights reserved.
//

#import "JNTHomeViewController.h"
#import "SDCycleScrollView.h"
#import "BaseCollectionViewCell.h"
#import "PersonalNeedCollectionViewCell.h"
#import "ApplyViewController.h"
#import "OfficialDocumentViewController.h"
#import "CalendarViewController.h"
#import "UIBarButtonItem+ZTItem.h"
#import "JNTDataRequestTools.h"
#import "homeModel.h"
#import "UIImage+Image.h"
#import "JNTMoreViewController.h"


@interface JNTHomeViewController ()<SDCycleScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,JNTMoreViewControllerDeleagte>

@property (nonatomic, strong) SDCycleScrollView *cycleScrollView; //滚动视图
@property (nonatomic, strong) UICollectionView *collectionView;   //分区一布局
@property (nonatomic, strong) NSMutableArray *imageArray;        //存储图片
@property (nonatomic, strong) NSMutableArray *titleArray;        //存储标题

@property (nonatomic, strong) UIButton *finshDeleteBtn;         //完成按钮
@property (nonatomic, assign) BOOL isDeleteFinsh;               //是否删除完成

@property (nonatomic, strong) NSMutableDictionary *parameter;   //参数字典

@property (nonatomic, assign) BOOL isAddData;                  //判断是否添加数据
@property (nonatomic, strong) NSMutableArray *addDataArray;    


@end

@implementation JNTHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpNavgationBar];

    [self.view addSubview:self.collectionView];
    
}

//- (void)tapAction:(UITapGestureRecognizer *)sender{
//    for (PersonalNeedCollectionViewCell *cell in self.collectionView.visibleCells) {
//        cell.deleteBtn.hidden = YES;
//    }
//}
#pragma Mark - -------UICollectionView的代理方法------

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    self.isDeleteFinsh = YES;
    self.finshDeleteBtn.hidden = YES;  //完成按钮隐藏
    [self.collectionView reloadData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 2;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {

        return self.titleArray.count;
    }else if (section == 1){
        
        return 1;
       
    }
   
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    PersonalNeedCollectionViewCell *CELL = [collectionView dequeueReusableCellWithReuseIdentifier:@"PersonalNeedCell" forIndexPath:indexPath];
    
    if (self.isDeleteFinsh) {
        
        CELL.deleteBtn.hidden = YES;
    }
    
    if (indexPath.section == 0) {
        
//        homeModel *model = self.titleArray[indexPath.row];
        CELL.cellLabel.text = self.titleArray[indexPath.row];
//        CELL.cellLabel.text = model.name;
        CELL.cellImageView.image = [UIImage imageNamed:[self.imageArray objectAtIndex:indexPath.row]];
        
        CELL.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
        
        UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
        [CELL addGestureRecognizer:longGesture];
        
    }else if (indexPath.section == 1){
        CELL.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
        CELL.cellLabel.text = @"更多";
        CELL.cellImageView.image = [UIImage imageNamed:@"ic_add"];
        CELL.deleteBtn.hidden = YES;
    }
    return CELL;
}


//长按cell删除数据
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer{
    
//    获取所有的cell
    for (PersonalNeedCollectionViewCell *cell in self.collectionView.visibleCells) {
        
            if (recognizer.state == UIGestureRecognizerStateEnded) {
                cell.deleteBtn.hidden = NO;
                
                self.isDeleteFinsh = NO;
                [cell.deleteBtn addTarget:self action:@selector(deleteCellAction:) forControlEvents:UIControlEventTouchUpInside];
        }
        
    }
    [self.collectionView reloadData];
}


- (void)deleteCellAction:(UIButton *)sender{
    if ([self.dataSourceMArr count]>0) {
        
        PersonalNeedCollectionViewCell *cell = (PersonalNeedCollectionViewCell *)[sender superview];//获取cell
        NSIndexPath *indexpath = [self.collectionView indexPathForCell:cell];//获取cell对应的indexpath;
        [self.titleArray removeObjectAtIndex:indexpath.row];
        NSArray *deleteItems = @[indexpath];
        [self.collectionView deleteItemsAtIndexPaths:deleteItems];
    }else{
        
        [_imageArray removeAllObjects];
    }
    [self.collectionView reloadData];
}

//完成删除按钮的回调方法
- (void)finshDeleteAction:(UIButton *)sender {
    
    for (PersonalNeedCollectionViewCell *cell in self.collectionView.visibleCells) {
        
        cell.deleteBtn.hidden = YES;
    }
    self.isDeleteFinsh = YES;
    sender.hidden = YES;
    [self.collectionView reloadData];
}



//注册头分区
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        if (indexPath.section == 0) {
            
            UIView *view = [[UIView alloc] initWithFrame:headerView.frame];
            if (self.isDeleteFinsh) {//是否点击完成按钮
                
                [view addSubview:self.cycleScrollView];
            }else {
                
                self.finshDeleteBtn = [UIButton buttonWithType:UIButtonTypeSystem];
                [self.finshDeleteBtn setTitle:@"完成" forState:UIControlStateNormal];
                [self.finshDeleteBtn setFrame:CGRectMake(5, view.frame.size.height-30, view.frame.size.width-10, 30)];
                [self.finshDeleteBtn addTarget:self action:@selector(finshDeleteAction:) forControlEvents:UIControlEventTouchUpInside];
                self.finshDeleteBtn.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.2];
                [view addSubview:self.finshDeleteBtn];
                [view addSubview:self.cycleScrollView];
            }
            [headerView addSubview:view];
        }
        return headerView;
    } else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        
        return nil;
    }
    return nil;
    
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
   
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0;

}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
        
        return UIEdgeInsetsMake(0, 0, 1, 1);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
         return CGSizeMake((kScreenWidth-2)/4, kScreenWidth/4);
    }
    else if (indexPath.section == 1){
        
        return CGSizeMake(80, 80);
    }
    return CGSizeMake(0, 0);
}
//头分区的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
    
        if (self.isDeleteFinsh) {
            
            return CGSizeMake(kScreenWidth, 210);
        }else {
            
            return CGSizeMake(kScreenWidth, 240);
        }
    }else{
        
        return CGSizeMake(0, 0);
    }
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            
            ApplyViewController *applyVC = [[ApplyViewController alloc] init];
            applyVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:applyVC animated:NO];
            
        }else if (indexPath.row == 1){
            
            
            OfficialDocumentViewController *officeVC = [[OfficialDocumentViewController alloc]init];
            officeVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:officeVC animated:NO];
            
            
        }else if (indexPath.row == 6){
            
            CalendarViewController *calendarVC = [[CalendarViewController alloc]init];
            calendarVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:calendarVC animated:NO];
            
            
        }

    } if (indexPath.section == 1){
        
        if (indexPath.row == 0){
        JNTMoreViewController *more = [[JNTMoreViewController alloc]init];
        more.delegate = self;
        self.isDeleteFinsh = YES;
        [self.navigationController pushViewController:more animated:YES];

        }
    }
    
}


//代理传值
- (void)passValue:(NSMutableArray *)dataArray {
    
    for (NSString *string in dataArray) {
        
        [self.titleArray addObject:string];
    }
    NSLog(@"%ld",self.addDataArray.count);
    [self.collectionView reloadData];
}

- (void)passImage:(NSMutableArray *)imageArray {
    
    for (NSString *string in imageArray) {
        
        [self.imageArray addObject:string];
    }
}

#pragma mark --滚动视图点击方法--
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    
    JNTLog(@"%ld",index);
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - SDCycleScrollView

- (SDCycleScrollView *)cycleScrollView{
    NSMutableArray *myArray = [NSMutableArray arrayWithObjects:[UIImage imageNamed:@"1.jpeg"],[UIImage imageNamed:@"2.jpeg"],[UIImage imageNamed:@"3.jpeg"], nil];
       if (!_cycleScrollView) {
        _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:kRect(0, 0, kScreenWidth,210) imageNamesGroup:myArray];
        _cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        _cycleScrollView.autoScroll = YES;
           _cycleScrollView.delegate = self;
        _cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
    }
    return _cycleScrollView;
}


#pragma mark - 设置导航条
- (void)setUpNavgationBar
{
    self.navigationController.navigationBar.translucent = NO;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"上栏"] forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.title = @"通号移动云平台";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationItem.rightBarButtonItem =  [UIBarButtonItem rightBarButtonItemTarget:self title:@"退出" action:@selector(rightBtnClick) forControlEvents:UIControlEventValueChanged];
}

//退出该应用
- (void)rightBtnClick
{
    JNTLog(@"确定退出应用将错过推送消息");
    [self.navigationController popViewControllerAnimated:YES];
}


//九宫格布局
- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-124) collectionViewLayout:flowLayout];
        [_collectionView registerNib:[UINib nibWithNibName:@"PersonalNeedCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PersonalNeedCell"];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}

- (NSMutableArray *)dataSourceMArr{
    
    if (!_dataSourceMArr) {
        
        _dataSourceMArr = [NSMutableArray array];
    }
    return _dataSourceMArr;
}

- (NSMutableArray *)addDataArray{
    
    if (!_addDataArray) {
        
        _addDataArray = [NSMutableArray array];
    }
    return _addDataArray;
}

//九宫格图片
- (NSMutableArray *)imageArray {
    
    if (!_imageArray) {
        
       _imageArray = [NSMutableArray arrayWithObjects:@"home_btn_approve_f",@"home_btn_workbench_f",@"home_btn_workbench_f",@"home_btn_signin_f", nil];
    }
    return _imageArray;
}


- (NSMutableArray *)titleArray {
    
    if (!_titleArray) {
        
        _titleArray = [NSMutableArray arrayWithObjects:@"申请",@"公文",@"新闻发布",@"签到", nil];
    }
    return _titleArray;
}

@end
